import axios from 'axios'

const service = axios.create({
  baseURL: `http://${location.hostname}:3000`,
  timeout: 20000
})

// response interceptor
service.interceptors.response.use(
  (response) => {
    //ajax返回数据通用处理，可以对指定错误码统一处理
    const res = response.data
    return res
    // if(res.code){
    //   if(res.code === '401'){
    //     store.dispatch('user/resetLoginStatus');
    //   }else{
    //     console.log("res err", res.code);
    //     Message({
    //       showClose: true,
    //       message: res.message,
    //       type: 'error'
    //     });
    //   }
    //   return Promise.reject(new Error(res.message || 'Error'))
    // }else{
    //   return res
    // }
  },
  (error) => {
    console.log('err' + error)
    // Message.error(error.message)
    return Promise.reject(error)
  }
)

export function getArticles() {
  return service({
    url: 'articles',
    method: 'get'
  })
}

export function getArticle(id: string) {
  return service({
    url: 'article/' + id,
    method: 'get'
  })
}

export function createArticle(data: Object) {
  return service({
    url: 'article',
    method: 'post',
    data: data
  })
}

export function updateArticle(id: string, data: Object) {
  return service({
    url: 'article/' + id,
    method: 'put',
    data: data
  })
}

export function deleteArticle(id: string) {
  return service({
    url: 'article/' + id,
    method: 'delete'
  })
}

export interface Article {
  id: number
  title: string
  description: string
  content: string
  userid: number
  html: string
  created_at: string
}
